package com.yuhang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuhang.entity.HyAdmin;
import com.yuhang.entity.HyBill;
import com.yuhang.entity.HyRepair;
import com.yuhang.entity.HyRepairStream;
import com.yuhang.service.HyAdminService;
import com.yuhang.service.HyRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuhang
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/hy-repair")
public class HyRepairController {
    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;

    @Autowired
    private HyRepairService hyRepairService;
    @Autowired
    private HyAdminService hyAdminService;
    @RequestMapping("/showAllRepair")
    public Map<String,Object> shoAllHyRepair(@RequestParam(value = "page",defaultValue = "1")int pageNum,
                                             @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                             @RequestParam(value = "likename",defaultValue = "")String likename,
                                             @RequestParam(value = "liketelephone",defaultValue = "")String liketelephone){
        IPage<HyRepair> page = hyRepairService.selectByPage(pageNum, pageSize,likename, liketelephone);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        map.put("msg","");
        return map;
    }

    @RequestMapping("/addRepair")
    public Map<String,Object> addHyRepair(HyRepair hyRepair){
        if(hyAdminService.selectByUsername(hyRepair.getUsername())){
            hyRepairService.save(hyRepair);
            Map<String, Object> map = new HashMap<>();
            map.put("code",0);
            return map;
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该用户名已存在！！！");
            return map;
        }
    }

    @RequestMapping("/updateRepair")
    public Map<String,Object> updateHyRepair(HyRepair hyRepair){
        String username = hyRepair.getUsername();
        Boolean aBoolean1 = hyAdminService.selectByUser(username);
        Boolean aBoolean2 = hyAdminService.selectByAdmin(username);
        HyRepair hyRepair1 = hyRepairService.getById(hyRepair.getId());
        QueryWrapper<HyRepair> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<HyRepair> list = hyRepairService.list(queryWrapper);
        Boolean aBoolean3 = false;
        if(list.size()>1){
            aBoolean3=false;
        }
        if(list.size()==1){
            HyRepair hyRepair2 = list.get(0);
            if(hyRepair2.getId()==hyRepair.getId()){
                aBoolean3=true;
            }
        }
        if(aBoolean1&&aBoolean2&&aBoolean3){
            hyRepairService.updateById(hyRepair);
            Map<String, Object> map = new HashMap<>();
            map.put("code",0);
            return map;
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该用户名已存在！！！");
            return map;
        }
    }
    @RequestMapping("/updateRepair2")
    public Map<String,Object> updateHyRepair2(HyRepair hyRepair,String newpassword,HttpSession session){
        String username = hyRepair.getUsername();
        Boolean aBoolean1 = hyAdminService.selectByUser(username);
        Boolean aBoolean2 = hyAdminService.selectByAdmin(username);
        HyRepair hyRepair1 = hyRepairService.getById(hyRepair.getId());
        QueryWrapper<HyRepair> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<HyRepair> list = hyRepairService.list(queryWrapper);
        Boolean aBoolean3 = true;
        if(list.size()>1){
            aBoolean3=false;
        }
        if(list.size()==1){
            HyRepair hyRepair2 = list.get(0);
            if(hyRepair2.getId()==hyRepair.getId()){
                aBoolean3=true;
            }else{
                aBoolean3=false;
            }
        }
        if(aBoolean1&&aBoolean2&&aBoolean3){
            if(hyRepair.getPassword().equals(hyRepair1.getPassword())){
                if(newpassword!=null&&newpassword!=""){
                    hyRepair.setPassword(newpassword);
                }
                hyRepairService.updateById(hyRepair);
                session.setAttribute("hyRepair",hyRepairService.getById(hyRepair));
                Map<String, Object> map = new HashMap<>();
                map.put("code",0);
                return map;
            }else {
                Map<String, Object> map = new HashMap<>();
                map.put("code",2);
                map.put("message","密码错误！！！");
                return map;
            }
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该用户名已存在！！！");
            return map;
        }
    }

    @RequestMapping("/deleteRepair")
    public Map<String,Object> deleteHyRepair(Integer id){
        hyRepairService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/uploadImage")
    public Map<String,Object> uploadHeadImg(MultipartFile file) throws IOException {//收参使用file
        file.transferTo(new File(path,file.getOriginalFilename()));
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg",file.getOriginalFilename());
        return map;
    }
}
